﻿package lobby
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.net.URLRequest;
	import flash.events.*;
	import lobby.events.LobbyEvent;
	
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	public class LoadMc extends EventDispatcher
	{
		public var myUrlRequest:URLRequest;
		public var myLoader:Loader;
		public var movieChild:Object;
		
		public function LoadMc()
		{
		
		}
		
		public function loadSwf(nameSwf:String, mc:MovieClip):void
		{
			myUrlRequest = new URLRequest(nameSwf);
			myLoader = new Loader();
			myLoader.load(myUrlRequest);
			if (mc.numChildren > 0)
				mc.removeChildAt(0);
			mc.addChild(myLoader);
			configureListeners(myLoader.contentLoaderInfo);
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(Event.INIT, initHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);
		}
		
		private function completeHandler(event:Event):void
		{
			//trace("completeHandler: " + event);
			movieChild = event.target.content;
			dispatchEvent(new LobbyEvent(LobbyEvent.MOVIE_LOADED, true));
		}
		
		private function httpStatusHandler(event:HTTPStatusEvent):void
		{
			//trace("httpStatusHandler: " + event);
		
		}
		
		private function initHandler(event:Event):void
		{
			//trace("initHandler: " + event);
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void
		{
		
			//trace("ioErrorHandler: " + event);
		
		}
		
		private function openHandler(event:Event):void
		{
			//trace("openHandler: " + event);
		}
		
		private function progressHandler(event:ProgressEvent):void
		{
			//trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
		}
		
		private function unLoadHandler(event:Event):void
		{
			//trace("unLoadHandler: " + event);
		}
	}

}